package team.seekers.hula.question.questionUser.service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import team.seekers.hula.common.entity.PageResult;
import team.seekers.hula.question.question.service.QuestionService;
import team.seekers.hula.question.questionUser.entity.dao.QuestionUserDO;
import team.seekers.hula.question.questionUser.entity.dto.QuestionUserManagePageDTO;
import team.seekers.hula.question.questionUser.entity.dto.QuestionUserUpdateDTO;
import team.seekers.hula.question.questionUser.entity.vo.QuestionUserManagePageVO;
import team.seekers.hula.question.questionUser.mapper.QuestionUserMapper;
import team.seekers.hula.user.user.entity.dao.UserDO;
import team.seekers.hula.user.user.service.UserService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 用户关注一个问题 服务实现类
 * </p>
 *
 * @author hbc
 * @since 2021-02-18
 */
@Service
public class QuestionUserManageService extends ServiceImpl<QuestionUserMapper, QuestionUserDO> {

    @Autowired
    @Lazy
    private QuestionService questionService;

    @Autowired
    @Lazy
    private UserService userService;


    public PageResult<QuestionUserManagePageVO> pageOrderTime(QuestionUserManagePageDTO queryDTO) {
        HashMap<String, QuestionUserDO> map = new HashMap<>();
        LambdaQueryChainWrapper<QuestionUserDO> lambdaQuery = lambdaQuery();
        if(queryDTO.getRole() != null) {
            lambdaQuery.eq(QuestionUserDO::getRole,queryDTO.getRole());
        }
        lambdaQuery.eq(QuestionUserDO::getQuestionId,queryDTO.getQuestionId());
        List<QuestionUserDO> list = lambdaQuery.list();
        if(list.size() == 0){
            return new PageResult<>();
        }
        List<String> userIds = new ArrayList<>();
        for (QuestionUserDO islandUserDO : list) {
            userIds.add(islandUserDO.getUserId());
            map.put(islandUserDO.getUserId(),islandUserDO);
        }



        // user表查询用户信息
        LambdaQueryChainWrapper<UserDO> userWrapper = userService.lambdaQuery();
        if(StringUtils.isNotBlank(queryDTO.getNickname())){
            userWrapper.likeRight(UserDO::getNickname,queryDTO.getNickname());
        }
        if(StringUtils.isNotBlank(queryDTO.getTel())){
            userWrapper.likeRight(UserDO::getTel,queryDTO.getTel());
        }
        userWrapper.in(UserDO::getId,userIds);
        Page<UserDO> page = userWrapper.page(new Page<>(queryDTO.getPageNow(), queryDTO.getPageSize()));

        List<QuestionUserManagePageVO> resList = new ArrayList<>();
        for (UserDO userDO : page.getRecords()) {
            QuestionUserDO questionUserDO = map.get(userDO.getId());
            QuestionUserManagePageVO build = QuestionUserManagePageVO.builder().id(questionUserDO.getId())
                    .nickname(userDO.getNickname())
                    .role(questionUserDO.getRole())
                    .tel(userDO.getTel())
                    .questionId(questionUserDO.getQuestionId())
                    .hulaScore(questionUserDO.getHulaScore())
                    .userId(userDO.getId())
                    .gmtCreate(questionUserDO.getGmtCreate()).build();
            resList.add(build);
        }
        return new PageResult<>(page.getTotal(),page.getSize(),resList);
    }


    public void updateRole(QuestionUserUpdateDTO questionUserUpdateDTO) {
        QuestionUserDO questionUserDO = new QuestionUserDO();
        questionUserDO.setId(questionUserUpdateDTO.getId());
        questionUserDO.setRole(questionUserUpdateDTO.getRole());
        updateById(questionUserDO);
    }
}
